package GreatBattleOfAircraft;

import GreatBattleOfAircraft.obj.*;
import GreatBattleOfAircraft.utils.GameUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Main extends JFrame implements Runnable {

    //游戏状态 0未开始 1游戏中 2暂停 3通关失败 4通关成功
    public static int state = 0;
    //游戏得分
    public static int score = 0;
    //双缓存图片
    Image offScreenImage = null;
    //窗口宽度
    int width = 550;
    //窗口高度
    int height = 900;

    int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
    int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;

    //游戏重回次数
    int count = 1;
    //敌机出现数量
    int enemyCount = 0;


    BgObj bgObj = new BgObj(GameUtils.bgImg,0,-900,1);

    public PlaneObj planeObj = new PlaneObj(GameUtils.phaneImg,230,-890,25,54,0,this);

    public BossObj bossObj = null;

    public Main(){
        lunch();
        new Thread(this).start();
    }

    public void lunch(){
        this.setResizable(false);//禁止窗体缩小放大
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击关闭按钮时关闭
        this.setVisible(true);//窗口可见
        this.setSize(width,height);//窗口大小
        this.setLocationRelativeTo(null);//窗口位置
        this.setTitle("小飞机大作战");//窗口标题

        GameUtils.gameObjList.add(bgObj);
        GameUtils.gameObjList.add(planeObj);

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 1 && state == 0){
                    state = 1;
                    repaint();
                }
            }
        });

        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == 32){//空格游戏暂停
                    switch (state){
                        case 1:
                            state = 2;
                            break;
                        case 2:
                            state = 1;
                            break;
                        default:
                    }
                }
                //2023.4.12
                // 以下功能是重新开始游戏
                if (e.getKeyCode() == 82){//按下R键重新开始
                    if (state == 3 || state == 4){
                        state = 0;
                        score = 0;
                        count = 1;
                        enemyCount = 0;
                        GameUtils.gameObjList.clear();
                        GameUtils.shellObjList.clear();
                        GameUtils.enemyObjList.clear();
                        GameUtils.boomObjList.clear();
                        GameUtils.removeObjList.clear();
                        Main.this.planeObj = new PlaneObj(GameUtils.phaneImg,230,-890,25,54,0,Main.this);
                        GameUtils.gameObjList.add(Main.this.bgObj);
                        GameUtils.gameObjList.add(Main.this.planeObj);
                        Main.this.bossObj = null;
                    }
                }
                //2023.4.12     增加按R重新开始游戏的功能结束
            }
        });



    }

    @Override
    public void paint(Graphics graphics){
        if (offScreenImage == null){
            offScreenImage = createImage(width,height);
        }

        Graphics graphics1 = offScreenImage.getGraphics();
        graphics1.fillRect(0,0,width,height);

        if (state == 0) {
            graphics1.drawImage(GameUtils.bgImg, 0, -height, this);
            graphics1.drawImage(GameUtils.bossImg, 150, 40, this);
            GameUtils.drawWord(graphics1,"点击开始游戏",Color.white,40,150,500);
            //2023.4.12新增
            GameUtils.drawWord(graphics1,"游戏过程中按空格可暂停游戏",Color.white,20,120,650);
            GameUtils.drawWord(graphics1,"合理使用暂停可实现瞬移哦～",Color.white,20,120,690);
            //2023.4.12新增结束

        }else if (state == 1){
            GameUtils.gameObjList.addAll(GameUtils.boomObjList);
            for (int i = 0; i< GameUtils.gameObjList.size(); i++){
                GameUtils.gameObjList.get(i).painSelf(graphics1);
            }
            GameUtils.gameObjList.removeAll(GameUtils.removeObjList);
            //System.out.println(GameUtils.gameObjList.size());检测场景内物体数量
        }
        if (state == 3){
            graphics1.drawImage(GameUtils.boomImg,planeObj.getX(),planeObj.getY(),100,100,this);
            GameUtils.drawWord(graphics1,"游戏失败",Color.white,40,190,500);
            //2023.4.12新增
            GameUtils.drawWord(graphics1,"按R后点击屏幕可重新开始游戏",Color.white,20,120,600);
            //2023.4.12新增结束
        }
        if (state == 4){
            // graphics1.drawImage(GameUtils.boomImg,bossObj.getX(),bossObj.getY(),100,100,this);
            GameUtils.drawWord(graphics1,"游戏通关",Color.white,40,190,500);
            //2023.4.12新增
            GameUtils.drawWord(graphics1,"按R后点击屏幕可重新开始游戏",Color.white,20,120,600);
            //2023.4.12新增结束
        }

        GameUtils.drawWord(graphics1,score+"分",Color.white,20,35,75);//积分面板
        graphics.drawImage(offScreenImage,0,0,this);
        count++;


    }


    void createObj(){
        //我方发射物
        if (count%15 == 0) {
            GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg, planeObj.getX() + 10, planeObj.getY() - 10, 10, 21, 5, this));
            GameUtils.gameObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size() - 1));
        }
        //敌方飞机
        if (count%20 == 0) {
            GameUtils.enemyObjList.add(new EnemyObj(GameUtils.enemyImg,(int) (Math.random()*13)*50,0,50,98,3,this));
            GameUtils.gameObjList.add(GameUtils.enemyObjList.get(GameUtils.enemyObjList.size()-1));
            enemyCount++;
        }
        //敌方Boss
        if (enemyCount > 5 &&bossObj == null){
            bossObj = new BossObj(GameUtils.bossImg,10,-290,236,290,1,this);
            GameUtils.gameObjList.add( bossObj);
        }
        //Boss发射物
        if (count%45 == 0&& bossObj!=null) {
            GameUtils.bulletObjList.add(new BulletObj(GameUtils.bulletImg,bossObj.getX()+110,bossObj.getY()+280,6,21,7,this));
            GameUtils.gameObjList.add(GameUtils.bulletObjList.get(GameUtils.bulletObjList.size() - 1));
        }
    }

    @Override
    public void run() {
        while (true){
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            if (state == 1){
                createObj();
                repaint();
            }

            try {
                Thread.sleep(30);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

